<template>
  <div class="test-container">
    <h2>图片上传组件测试</h2>
    
    <div class="test-section">
      <h3>营业执照上传测试</h3>
      <ImageUpload
        :image-url="businessLicense"
        @update:image-url="businessLicense = $event"
        :accept="'image/*'"
        :size-limit="5"
        :upload-hint="'支持 JPG、PNG 格式，大小不超过 5MB'"
      />
      
      <div v-if="businessLicense" class="result">
        <h4>上传结果：</h4>
        <p>图片已上传成功</p>
        <p>图片大小：{{ getImageSize(businessLicense) }}</p>
      </div>
    </div>

    <div class="test-section">
      <h3>普通图片上传测试</h3>
      <ImageUpload
        :image-url="normalImage"
        @update:image-url="normalImage = $event"
        :accept="'image/*'"
        :size-limit="2"
        :upload-hint="'支持 JPG、PNG 格式，大小不超过 2MB'"
      />
      
      <div v-if="normalImage" class="result">
        <h4>上传结果：</h4>
        <p>图片已上传成功</p>
        <p>图片大小：{{ getImageSize(normalImage) }}</p>
      </div>
    </div>

    <div class="actions">
      <el-button @click="clearAll">清空所有图片</el-button>
      <el-button type="primary" @click="showResults">显示结果</el-button>
    </div>

    <div v-if="showResultsFlag" class="results">
      <h3>所有上传的图片：</h3>
      <div class="image-grid">
        <div v-if="businessLicense" class="image-item">
          <h4>营业执照</h4>
          <img :src="businessLicense" alt="营业执照" />
        </div>
        <div v-if="normalImage" class="image-item">
          <h4>普通图片</h4>
          <img :src="normalImage" alt="普通图片" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import ImageUpload from '@/components/common/ImageUpload.vue'

const businessLicense = ref('')
const normalImage = ref('')
const showResultsFlag = ref(false)

const getImageSize = (imageUrl) => {
  if (!imageUrl) return '0 KB'
  
  // 计算 base64 字符串的大小
  const base64Length = imageUrl.length - (imageUrl.indexOf(',') + 1)
  const sizeInBytes = (base64Length * 3) / 4
  const sizeInKB = Math.round(sizeInBytes / 1024)
  const sizeInMB = (sizeInKB / 1024).toFixed(2)
  
  if (sizeInKB > 1024) {
    return `${sizeInMB} MB`
  } else {
    return `${sizeInKB} KB`
  }
}

const clearAll = () => {
  businessLicense.value = ''
  normalImage.value = ''
  showResultsFlag.value = false
  ElMessage.success('所有图片已清空')
}

const showResults = () => {
  showResultsFlag.value = true
  ElMessage.info('显示上传结果')
}
</script>

<style scoped>
.test-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.test-section {
  margin-bottom: 40px;
  padding: 20px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
}

.test-section h3 {
  margin-top: 0;
  color: #2c3e50;
}

.result {
  margin-top: 20px;
  padding: 15px;
  background: #f0f9ff;
  border-radius: 6px;
  border-left: 4px solid #4A90E2;
}

.result h4 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.result p {
  margin: 5px 0;
  color: #7f8c8d;
}

.actions {
  text-align: center;
  margin: 30px 0;
}

.results {
  margin-top: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.image-item {
  text-align: center;
}

.image-item h4 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.image-item img {
  max-width: 100%;
  max-height: 200px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>
